package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;
	@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {
		//通过页面对象 将page rows进行参数封装
		IPage iPage = new Page(page,rows);
		QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("updated");
		//通过iPage对象封装其他的分页数据
		iPage = itemMapper.selectPage(iPage, queryWrapper);
		long total =iPage.getTotal();
		List<Item> itemList = iPage.getRecords();
		return  new EasyUITable(total,itemList);
	}
	@Transactional //标记方式使用事务控制
	@Override
	public void saveItem(Item item, ItemDesc itemDesc) {
		item.setStatus(1);//设定启动状态
		itemMapper.insert(item);

		itemDesc.setItemId(item.getId());
		itemDescMapper.insert(itemDesc);
	}

	@Override
	@Transactional
	public void updateItem(Item item, ItemDesc itemDesc) {

		itemMapper.updateById(item);
		itemDesc.setItemId(item.getId());
		itemDescMapper.updateById(itemDesc);
	}

	@Override
	@Transactional
	public void deleteItems(Long[] ids) {
		//删除商品信息
		itemMapper.deleteIds(ids);
		//删除商品详情信息
		List<Long> idList =Arrays.asList(ids);
		itemDescMapper.deleteBatchIds(idList);
		

	}
	@Transactional
	@Override
	public void updateStatus(Integer status, Long[] ids) {
		//参数1：实体对象，将要修改的数据封装
		Item item = new Item();
		item.setStatus(status);
		//参数2：条件构造器，动态拼接where条件
		UpdateWrapper updateWrapper = new UpdateWrapper();
		List<Long> idList=Arrays.asList(ids);
		updateWrapper.in("id",idList);
		itemMapper.update(item,updateWrapper);
	}

	@Override
	public ItemDesc findItemDescById(Long id) {

		return itemDescMapper.selectById(id);
	}


//		//通过页面对象 将page rows进行参数封装
//		IPage  iPage=new Page(page,rows);
//		QueryWrapper<Item>queryWrapper = new QueryWrapper<>();
//		queryWrapper.orderByDesc("updated");
//		 //通过iPage对象封装其他的分页数据
//		iPage=itemMapper.selectPage(iPage,queryWrapper);
//		long total = iPage.getTotal();
//		List<Item> itemList = iPage.getRecords();
//		return new EasyUITable(total,itemList);
	}


//	@Override
//	public EasyUITable findItemByPage(Integer page, Integer rows) {
//		//获取总记录数
//		long total = itemMapper.selectCount(null);
//		//分页查询
//		int startIndex=(page-1)*rows;
//		List<Item> pageList=itemMapper.findItemByPage(startIndex,rows);
//		return new EasyUITable(total,pageList);
//	}

